<template>
    <div>
        <el-form  :model="form"   ref="ruleForm" label-width="80px" :rules="rules">
            <el-form-item label="手机号" prop="telephoneNumber">
                <el-input v-model="form.telephoneNumber"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input  type="password"  autocomplete="off" v-model="form.password"></el-input>
            </el-form-item>

            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="form.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="form.sex" prop="sex">
                    <el-radio label="男">男</el-radio>
                    <el-radio label="女">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="生日" >
                <el-date-picker
                        v-model="form.birth"
                        type="date"
                        placeholder="选择日期"
                        value-format="yyyy-MM-dd"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item label="医生凭证">
                <el-upload
                        class="upload-demo"
                        :show-file-list="true"
                        action="http://localhost:8888//upload/file"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <el-button  style="margin-left: 105px;" size="small" type="primary">点击上传</el-button>
                </el-upload>
            </el-form-item>

        </el-form>


        <div class="btn">
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button type="success" @click="zhuce()">确 定</el-button>
        </div>

    </div>
</template>

<script>
    export default {
        name: "yishengzhuce",
        data(){
            var checkmail = (rule, value, cb) => {
                var RegEmail =  /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/
                if(RegEmail.test(value)){return cb()}
                return cb(new Error('请输入合法的邮箱'))
            }
            var checkphone = (rule, value, cb) => {
                var RegEmail = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/

                if(RegEmail.test(value)){return cb()}
                return cb(new Error('请输入合法的手机号'))
            }

            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.checkPass !== '') {
                        this.$refs.form.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return{
                form:{
                    username:'',
                    sex:'男',
                    password:'',
                    role: '医生',
                    email:'',
                    birth:'',
                    telephoneNumber:'',
                    proof:''
                },
                rules:{
                    // username:[
                    //     { required: true, message: '请输入用户名', trigger: 'blur' },
                    //     { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 8, message: '密码长度为6-8', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { required: true, message: '确认密码', trigger: 'blur' },
                        { validator: validatePass2, trigger: 'blur' },
                    ],
                    email:[
                        { required: true, message: '请输入邮箱', trigger: 'blur' },
                        { validator:checkmail,trigger: 'blur' }],
                    telephoneNumber:[
                        { required: true, message: '请输入手机号', trigger: 'blur' },
                        { validator:checkphone,trigger: 'blur' }],
                },
                fileList: []
            }
        },
        methods:{
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            open() {
                this.$alert('注册成功！请等待管理员审批您的医生身份', '提示', {
                    confirmButtonText: '确定'
                });
            },
            zhuce(){
                this.$refs.ruleForm.validate(valid =>{
                    if(!valid)return
                    this.$http.post('user/register', this.form).then(response => {
                        console.log(response);
                        if (response.data.code === 200) {

                            this.$refs.ruleForm.resetFields();
                            this.open()
                        }
                    })
                })

            },

            async handleAvatarSuccess (res) {
                // this.form.proof = URL.createObjectURL(file.raw);
                this.form.proof =  res.data

            },
            beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                    this.$message.error('上传文件大小不能超过 2MB!');
                }
                return  isLt2M;
            }
        }
    }
</script>

<style scoped>
    .btn{
        margin-left: 300px;
    }
</style>
